'''
用于发送请求的模块,
'''
import allure
import pytest

from Common import log_opertion
from Common.yaml_opertion import YamlOpertion
import requests

class SendRequests(object):
    def __init__(self):
        self.logs=log_opertion.logs
        self.session=requests.session()
        self.yo=YamlOpertion()

    def send_request(self,**kwargs):
        try:
            # 发起请求
            response=self.session.request(**kwargs)

            # 处理特殊格式:request请求后返回的cookies
            Cookies={}
            set_cookies=requests.utils.dict_from_cookiejar(response.cookies)
            # 如果响应携带cookies,就处理并写入extract.yml
            if set_cookies:
                Cookies['cookies']=set_cookies
                self.yo.write_yaml(value=Cookies,filename='extract.yml')
                self.logs.info(f'Cookies:{set_cookies}')

            return response

        except requests.exceptions.ConnectionError as e:
            self.logs.error('接口连接服务异常:',e)
            pytest.fail('测试时接口连接服务异常:', e)
        except requests.exceptions.HTTPError as e:
            self.logs.error('http异常:',e)
            pytest.fail('测试时http异常:',e)
        except requests.exceptions.RequestException as e:
            self.logs.error('请求异常:',e)
            pytest.fail('测试时请求异常:',e)


    # 负责接收处理好的格式参数记录并发起请求
    def run_main(self,url,method=None,apiname=None,casename=None,params=None,data=None,json=None,headers=None,verify=False,file=None,cookies=None,**kwargs):
        self.logs.info(f'接口名称:{apiname}')
        self.logs.info(f'用例名称:{casename}')
        self.logs.info(f'请求方法:{method}')
        self.logs.info(f'请求路径:{url}')
        self.logs.info(f'请求头:{headers}')

        # allure附件添加测试数据方便报告查看
        allure.attach(body=apiname,name=f'接口名称:{apiname}')
        allure.attach(body=casename, name=f'用例名称:{casename}')
        allure.attach(body=method,name=f'请求方法:{method}')
        allure.attach(body=url,name=f'请求接口:{url}')
        allure.attach(body=str(headers),name=f'请求头:{str(headers)}')
        if cookies:
            self.logs.info(f'请求携带cookies:{cookies}')
            allure.attach(body=str(cookies), name=f'请求携带cookies')

        if params:
            res = self.send_request(params=params,url=url, method=method, headers=headers, cookies=cookies,verify=verify,**kwargs)
        elif data:
            res = self.send_request(data=data,url=url, method=method, headers=headers, cookies=cookies,verify=verify,**kwargs)
        elif json:
            res = self.send_request(json=json,url=url, method=method, headers=headers, cookies=cookies,verify=verify,**kwargs)
        else:
            res = self.send_request(json=json, url=url, method=method, headers=headers, cookies=cookies, verify=verify,**kwargs)

        return res
        # elif data:
        #     res = self.send_request(url=url, method=method, header=header, data=data,cookies=cookies,verify=verify,file=file,**kwargs)
        # elif json:
        #     res = self.send_request(url=url, method=method, header=header, json=json,cookies=cookies,verify=verify,file=file,**kwargs)


if __name__ == '__main__':
    login_url = 'http://127.0.0.1:8787/dar/user/login'
    data = {'user_name': 'test01', "passwd": "admin123"}
    send=SendRequests()
    res=send.run_main(method='post',data=data,url=login_url)
    print(res)
